/**
 * UI Controller
 */
var controller = {
	togglePlay : function(key) {
		debug.print("controller.togglePlay [key: " + key + "]");
		player[key].togglePlay();
	},
	
	faderChange : function() {
		var value = $("#fader-container").slider('option', 'value');
		if (value < 5) {
			$("#fader-container").slider('option', 'value', 0);
		} else if (value > 95) {
			$("#fader-container").slider('option', 'value', 100);
		} else if (value < 55 && value > 45) {
			$("#fader-container").slider('option', 'value', 50);
		}
		value = $("#fader-container").slider('option', 'value');
		debug.print("controller.faderChange [value: " + value + "]");
		player[0].setVolume(Math.min((100 - value) * 2, 100));
		player[1].setVolume(Math.min(value * 2, 100));
	},
	
	faderReset : function() {
		debug.print("controller.faderReset");
		$("#fader-container").slider('option', 'value', 50);
		player[0].setVolume(100);
		player[1].setVolume(100);
	},
	
	positionChange : function(key) {
		debug.print("controller.positionChange [key: " + key + "]");
		var value = $("#vid" + key + "-slider-bar").slider('option', 'value');
		debug.print("controller.positionChange [key: " + key + ", value: " + value + "]");
		player[key].setPosition(value);
	},
	
	doVideoSearch : function() {
		debug.print("controller.doVideoSearch");
		var q = $("#search-field > :input").val();
		if (!q) {
			flash.flash("No search","query given");
			return false;
		}
		debug.print("controller.doSearch [q: " + q + "]");
		search_videos.load(q);
	},
	
	beLucky : function(search, callback) {
		debug.print("controller.beLucky");
		var q = { artist : search };
		autodj.initialized = false;
		autodj.addSimilar(q, function(status) {
			callback(status);
			autodj.enable();			
		});
	},
	
	cue : function(key, item) {
		debug.print("controller.cue [key: " + key + ", item: " + item + "]");
		player[key].cue(item);
	},
	
	addToNp : function(item) {
		debug.print("controller.addToNp [item: " + item + "]");
		np.add(item);
	},
	
	removeFromNp : function(dom, item) {
		debug.print("controller.removeFromNp [dom: " + dom + ", item: " + item + "]");
		np.remove(dom, item);
	},
		
	toggleAutodj : function() {
		debug.print("controller.toggleAutodj");
		if (autodj.isEnabled()) {
			autodj.disable();
		} else {
			autodj.enable();
		}
	},
	
	cueNext : function(key) {
		debug.print("controller.cueNext [key: " + key + "]");
		if ($(".autodj-next" + key).hasClass("disabled")) {
			return false;
		}
		autoplay.loadNext(key);
	},
	
	mixNow : function() {
		debug.print("controller.mixNow");
		autoplay.fadeStart();
	}
}